
#ifndef RF_DEVICE_H
#define RF_DEVICE_H




#include "RF_socketPort/IOCPS.h"
#include "RF_serialPort/CnComm.h"
#include "RF_serialPort/SerialPort.h"

typedef void (*NotifyLogBufferFuc )( char *strLog );
typedef void (*NotifyNewConnectionFuc )( char *ip);
typedef void (*NotifyDisConnectionFuc )( char *ip);
typedef void (*NotifyRecvNewDataFuc )( int port, int type, char *newData, int uLen );
typedef void (*NotifyWirtCompletedFuc)( int port, int type, char *newData, int uLen );
enum{ SOCKET_PORT,SEARIL_PORT};
class RF_Device :public IOCPS,public CnComm{

public:
	RF_Device();
	~RF_Device();

public:
	 int  RF_Startup( int type = SOCKET_PORT,int iPara0 = 6800 ,int iPara1 = 1201 );
	 void RF_ShutDown();
	 void RF_SetDlgHwnd( HWND hWnd);
	 void RF_SetDeviceType( int type );
	 void RF_SetNotifyLogBuffer( NotifyLogBufferFuc fuc );
	 void RF_SetNotifyNewConnectionFuc( NotifyNewConnectionFuc fuc );
	 void RF_SetNotifyDisConnectionFuc( NotifyDisConnectionFuc fuc );
	 void RF_SetNotifyRecvNewDataFuc( NotifyRecvNewDataFuc fuc );
	 void RF_SetNotifyWirtCompletedFuc( NotifyWirtCompletedFuc fuc );
	 int  RF_DeviceSendData(  int port,char*  pSendData, int uSendLen,BOOL bEnd );
private:
	virtual inline void ProcessJob(JobItem* pJob, IOCPS* pServer);
	virtual void AppendLog(const char* fmt, ...);
	virtual void NotifyNewConnection(ClientContext* pcontext);
	virtual void NotifyDisconnectedClient(ClientContext* pContext);
	virtual void NotifyReceivedPackage(CIOCPBuffer* pOverlapBuff,
									   int nSize, ClientContext* pContext);	
	virtual void NotifyWriteCompleted(ClientContext* pContext, 
										DWORD dwIoSize, CIOCPBuffer* pOverlapBuff);

	virtual void CommNotifyRecvNewData(unsigned char *pNewData, int NewLen);
	virtual void CommNotifySendDataCompleted(unsigned char *pNewData, int NewLen,BOOL bOK );
	virtual void CommNotifySendDataOneFrame(unsigned char *pNewData, int NewLen,BOOL bOK );
	virtual BOOL SetWorkers(int nThreads);
	BOOL         AddJob(JobItem* pJob);
	BOOL         AddJob(int port, unsigned char uRepID);
	JobItem *    GetJob();
	static UINT  WorkerThreadProc(LPVOID pParam);
	void         ShutDownWorkers();
	inline void	 FreeJob(JobItem* pJob);
	DWORD        WrtieSerialPort( unsigned char *pData, unsigned int len );
	void		 OnReceive();

	//int Push_DeviceBuffer( unsigned char *pData, int uLen );

private:
	
	 HWND m_hWnd;
	 int  m_type;
private:
	 NotifyLogBufferFuc			m_NotifyLogBufferFuc;
	 NotifyNewConnectionFuc     m_NotifyNewConnectionFuc;
	 NotifyDisConnectionFuc     m_NotifyDisConnectionFuc;
	 NotifyRecvNewDataFuc		m_NotifyRecvNewDataFuc;
	 NotifyWirtCompletedFuc     m_NotifyWirtCompletedFuc;

	 int m_nOfWorkers;
	 // <threadID, CWinThread*>
	 // CMapWordToPtr m_WorkerThreadMap;
	 ThreadVector m_WorkerVector;
	 MyLock m_WorkerVectorLock; // guadian

	 MyLock m_JobQueueLock;
	 JobQueue m_JobQueue; // containing <JobItem*>

	 DeviceBuffer  m_DevBuffer[DEVICENUM_MAX];
};
#endif